﻿// ———————————————————————–
// <copyright file="MainForm.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// MainForm.cs - Main Class for the RouterTool WinForm
// Project: EDXLSharp_Tools- RouterTool
//
// Language:    C#, .NET 4.0
// Platform:    Windows 7, VS 2010
// Author:      Don McGarry The MITRE Corporation
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RouterTool
{
  public partial class RouterMain : Form
  {
    RuleTool childRuleToolForm;

    public RouterMain()
    {
      InitializeComponent();
    }

    private void buttAdd_Click(object sender, EventArgs e)
    {
      if (comboVLURI.Text == "Select...")
      {
        MessageBox.Show("Choose a VL!");
        return;
      }
      else if (textValue.Text == "Value...")
      {
        MessageBox.Show("Type in a value!");
        return;
      }
      else if (textDesc.Text == "Description")
      {
        MessageBox.Show("Type in a desc");
      }

      ValueListLinQDataContext linqdb = new ValueListLinQDataContext();
      IC_NETValueList entry = new IC_NETValueList();
      if (!String.IsNullOrEmpty(textCommandStruct.Text) || textCommandStruct.Text != "Command Structure...")
      {
        entry.CommandStructure = textCommandStruct.Text;
      }

      entry.Description = textDesc.Text;
      entry.Value = textValue.Text;
      if (newURITextBox.Enabled)
      {
        entry.ValueListURI = newURITextBox.Text;
      }
      else
      {
        entry.ValueListURI = comboVLURI.Text;
      }
      entry.VLKey = Guid.NewGuid();
      linqdb.IC_NETValueLists.InsertOnSubmit(entry);
      linqdb.SubmitChanges();
      MessageBox.Show("Done");
    }

    private void buttRuleTool_Click(object sender, EventArgs e)
    {
      childRuleToolForm = new RuleTool();
      childRuleToolForm.Show();
    }

    private void refreshButt_Click(object sender, EventArgs e)
    {
      ValueListLinQDataContext linqdb = new ValueListLinQDataContext();
      var uris = (from p in linqdb.IC_NETValueLists
                 select p.ValueListURI).Distinct();
      comboVLURI.Items.Clear();
      comboVLURI.Text = "Select...";
      foreach (string uri in uris)
      {
        comboVLURI.Items.Add(uri);
      }
      comboVLURI.Items.Add("New Uri...");
    }

    private void comboVLURI_SelectedIndexChanged(object sender, EventArgs e)
    {
      valueListBox.Items.Clear();
      newURITextBox.Enabled = false;
      if (comboVLURI.Text == "New Uri...")
      {
        newURITextBox.Enabled = true;
      }
      else if (comboVLURI.Text != "Select...")
      {
        try
        {
          ValueListLinQDataContext linqdb = new ValueListLinQDataContext();
          var values = (from p in linqdb.IC_NETValueLists
                        where p.ValueListURI == comboVLURI.Text
                        select p.Value);
          foreach (string value in values)
          {
            valueListBox.Items.Add(value);
          }
        }
        catch (Exception ex)
        {
          MessageBox.Show(ex.ToString(), "Error Getting Values", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
      }
    }

    private void RouterMain_Load(object sender, EventArgs e)
    {
      this.refreshButt_Click(this, e);
    }
  }
}
